#include "cgeDatagramPacket.h"

namespace basterd
{
	namespace net
	{
	    // ----------------------------------------------------------------------------
		// DatagramPacket
		// ----------------------------------------------------------------------------
		DatagramPacket::DatagramPacket( muint len )
			: buffer(0), length(len), address()
		{
			this->buffer = new char[length];
			memset(buffer, 0, length);
			memset(&address,0,sizeof(address));
		}
		// ----------------------------------------------------------------------------
		DatagramPacket::DatagramPacket( muint len, const sockaddr_in& adr, unsigned short prt )
			: buffer(0), length(len), address()
		{
			this->buffer = new char[length];
			memset(buffer, 0, length);

			memcpy(&address,&adr,sizeof(address));
		}
		// ----------------------------------------------------------------------------
		DatagramPacket::DatagramPacket(  muint len,
			const InetAddress& inet, unsigned short prt )
			: buffer(0), length(len), address()
		{
			this->buffer = new char[length];
			memset(buffer, 0, length);

			inet.asSockAddress( address, prt );
		}
		// ----------------------------------------------------------------------------
		DatagramPacket::DatagramPacket( muint len, const String& adr, mushort port )
			: buffer(0), length(len), address()
		{
			this->buffer = new char[length];
			memset(buffer, 0, length);

			memset(&address,0,sizeof(address));
			address.sin_family = AF_INET;
			address.sin_addr.s_addr = inet_addr( adr.c_str() );
			address.sin_port = htons(port);
		}
		// ----------------------------------------------------------------------------
		DatagramPacket::DatagramPacket( const char* from, muint len, const sockaddr_in& adr, unsigned short prt )
			: buffer(0), length(len), address()
		{
			this->buffer = new char[length];
			memcpy(buffer, from, length);

			memcpy(&address,&adr,sizeof(address));
		}
		// ----------------------------------------------------------------------------
		DatagramPacket::DatagramPacket( const char* from, muint len,
				                        const InetAddress& inet, unsigned short prt )
			: buffer(0), length(len), address()
		{
			this->buffer = new char[length];
			memcpy(buffer, from, length);

			inet.asSockAddress( address, prt );
		}
		// ----------------------------------------------------------------------------
		DatagramPacket::DatagramPacket( const char* from, muint len,
										const String& adr, unsigned short port )
			: buffer(0), length(len), address()
		{
			this->buffer = new char[length];
			memcpy(buffer, from, length);

			memset(&address,0,sizeof(address));
			address.sin_family = AF_INET;
			address.sin_addr.s_addr = inet_addr( adr.c_str() );
			address.sin_port = htons(port);
		}
		// ----------------------------------------------------------------------------
		DatagramPacket::~DatagramPacket(void)
		{
			delete [] buffer;
			buffer = 0;
			length = 0;
			memset(&address,0,sizeof(address));
		}
		// ----------------------------------------------------------------------------
		void DatagramPacket::setBroadcast(mushort prt)
		{
			InetAddress::INET_BROADCAST.asSockAddress( address, prt );
		}
		// ----------------------------------------------------------------------------
		String DatagramPacket::getAddress(void) const
		{
			return ::inet_ntoa( address.sin_addr );
		}		
		// ----------------------------------------------------------------------------
		void DatagramPacket::setAddress(const String& ipAddress)
		{
			address.sin_addr.s_addr = inet_addr( ipAddress.c_str() );
		}
		// ----------------------------------------------------------------------------       
		mushort DatagramPacket::getPort(void) const
		{
			return ntohs( address.sin_port );
		}
		// ----------------------------------------------------------------------------       
		void DatagramPacket::setPort(mushort port)
		{
			address.sin_port = htons(port);
		}
        // ----------------------------------------------------------------------------       
        
	}
}
